﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.Model;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class EditPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PostService service = new PostService();
                PostModel model = service.GetPostModel(Request.QueryString[Constants.QUERYSTR_POST_ID]);

                TitleTxtBox.Text = model.Title;
                ContentTxtBox.Text = model.Content;
                SourceTxtBox.Text = model.Source;
                KeywordsTxtBox.Text = model.Keyword;
                TargetDDL.SelectedValue = model.Target;
                authorLbl.Text = model.Author;
                if (model.Status.Equals("有效"))
                {
                    StatusRadioButtonList.SelectedIndex = 0;
                }
                else
                {
                    StatusRadioButtonList.SelectedIndex = 1;
                }
                publishDateLbl.Text = model.PublishDate;

                PostTypeDDL.DataSource = service.GetAllPostTypes();
                PostTypeDDL.DataTextField = "Name";
                PostTypeDDL.DataValueField = "ID";
                PostTypeDDL.DataBind();
                PostTypeDDL.SelectedValue = model.PostTypeID;
            }
        }

        protected void PublisBtn_Click(object sender, EventArgs e)
        {
            PostService service = new PostService();
            Post postToUpdate = service.GetPost(Request.QueryString[Constants.QUERYSTR_POST_ID]);
            postToUpdate.Title = TitleTxtBox.Text;
            postToUpdate.Content = ContentTxtBox.Text;
            postToUpdate.Keyword = KeywordsTxtBox.Text;
            postToUpdate.Source = SourceTxtBox.Text;
            postToUpdate.Status = StatusRadioButtonList.SelectedIndex == 0 ? "有效" : "无效";
            postToUpdate.TargetType = TargetDDL.SelectedValue;
            //newPost.PublishDate = DateTime.Now;
            postToUpdate.PostType = service.GetPostType(PostTypeDDL.SelectedValue);
            postToUpdate.AuthorID = ContextService.CurrentAccount.UserID;
            postToUpdate.LastUpdate = DateTime.Now;
            service.UpdatePost(postToUpdate);
            Response.Write("<script language='JavaScript'> alert( '公告已成功更新！');location.href= 'ManagePost.aspx' </script> "); 
        }

        protected void TaskFileGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                ((HyperLink)e.Row.FindControl("ViewFileHyperLink")).NavigateUrl = string.Format("~/PostFile/{0}_{1}", ((PostFile)e.Row.DataItem).ID, ((PostFile)e.Row.DataItem).FileName);
            }
        }

        protected void ModalPopupExtender1_Load(object sender, EventArgs e)
        {
            PostService postService = new PostService();
            TaskFileGridView.DataSource = postService.GetPostFiles(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID])).ToList();
            TaskFileGridView.DataBind();
        }

        protected void uploadTaskFileBtn_Click(object sender, EventArgs e)
        {
            if (TaskFileFileUpload.HasFile)
            {
                PostService postService = new PostService();
                long fileID = postService.AddPostFile(TaskFileFileUpload.FileName, Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]));
                TaskFileFileUpload.SaveAs(Server.MapPath(string.Format("PostFile/{0}_{1}", fileID, TaskFileFileUpload.FileName)));

                IQueryable<PostFile> files = postService.GetPostFiles(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]));
                TaskFileGridView.DataSource = files.ToList();
                TaskFileGridView.DataBind();

                TaskFileModalPopupExtender.Show();
            }
        }

        protected void taskFileLnkBtn_Click(object sender, EventArgs e)
        {
            PostService postService = new PostService();
            TaskFileGridView.DataSource = postService.GetPostFiles(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID])).ToList();
            TaskFileGridView.DataBind();

            TaskFileModalPopupExtender.Show();
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<PostFile> dds = new List<PostFile>();
            dds.Add(new PostFile());
            return dds;
        }

        protected void securityLnkBtn_Click(object sender, EventArgs e)
        {
            PostService postService = new PostService();
            List<PostSecurityModel> securityModels = postService.GetPostSecurityModelByPostID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]));
            PostSecurityGridview.DataSource = securityModels;
            PostSecurityGridview.DataBind();
            PostSecurityModalpopupextender.Show();     
        }

        protected void PostSecurityGridview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                ((CheckBox)e.Row.FindControl("securityChkBox")).Attributes["UserTypeID"] = (e.Row.DataItem as PostSecurityModel).UserTypeID.ToString();
            }
        }

        protected void SecurityCheck_Clicked(Object sender, EventArgs e)
        {
            CheckBox securityChkBox = (CheckBox)sender;
            int userTypeID = Convert.ToInt32(securityChkBox.Attributes["UserTypeID"]);
            PostService postService = new PostService();
            if (securityChkBox.Checked)
            {
                postService.AddPostSecurity(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]), userTypeID);
            }
            else
            {
                postService.DeletePostSecurity(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]), userTypeID);
            }
            List<PostSecurityModel> securityModels = postService.GetPostSecurityModelByPostID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]));
            PostSecurityGridview.DataSource = securityModels;
            PostSecurityGridview.DataBind();
            PostSecurityModalpopupextender.Show();   
        }
    }
}
